package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/16 23:07
 **/
public class Day31_6_Num59_GenerateMatrix {
    class Solution {
        public int[][] generateMatrix(int n) {
            int[][] data = new int[n][n];
            int l = 0, r = n-1, u = 0, d = n-1;
            int num = 1;
            while(true){
                for(int i = l;i <= r;i++) data[u][i] = num++;

                if(++u > d) break;
                for(int i = u;i <= d;i++) data[i][r] = num++;

                if(--r < l) break;
                for(int i = r;i >= l;i--) data[d][i] = num++;

                if(--d < u) break;
                for(int i = d;i >= u;i--) data[i][l] = num++;

                if(l++ > r) break;
            }
            return data;
        }
    }
}